#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
####################################################################
#
#   lsswcomp command
#
#   Syntax:  lsswcomp { -h | -help }
#
#    -h | -help              - Displays the syntax of the command 
#
####################################################################
#
#   Initial version - 3/15/04
#   Wesley D. Jones
####################################################################

################################################################################
#
# start main body of code
#
################################################################################
$PRGRM  = "lsswcomp";                         #program name
$BIN = "/opt/hsc/bin";                        #program location
$chswnm_Q = "/opt/hsc/bin/chswnm -q";           #query daemon status
$chswnm_C = "/opt/hsc/bin/chswnm -c";           #activate startFNMD -h
$chswnm_D = "/opt/hsc/bin/chswnm -d";           #deactivate daemon
$i_stub_FS = "/opt/hsc/bin/i_stub_FS fnm -g"; #i_stub_FS command
$USAGE = "Usage: lsswcomp { -h | --help }\n";          #usage statement

require "/opt/hsc/bin/i_stub_msg.pl";		#used to print correct msg from i_stub

#easy way to get command line options
use Getopt::Long;

#routine to exit the script
sub exit_routine {

if ($daemon != 1)
{
	#if the daemon was already running dont kill (defect 109668), else if coming from fnmd -h kill
	$chswnm_return = `$chswnm_D`;             #terminate the fnmd
}

if ($i_stub_flag == 1)  #some type of i_stub_error return 2
{
	$ret = 2;
}
elsif ($chswnm_return != 0)  #daemon was running, couldn't execute    #returns 0 if successful
{
        $ret =2;
}
else				#everything went ok
{
	$ret =0;
}
exit $ret;

} # end exit_routine subroutine

Getopt::Long::Configure("no_ignore_case");   #ignore case for command line options
$result = GetOptions(                   #get various options off the command line
        "h" => \$help,
	"help" => \$help,
);
#----------------------------------------------------------------------------------#
if ($help) {                            #if -h print USAGE and exit with rc=0
        print $USAGE;      #USAGE
        $ret = 0;
	$daemon = 1;
        &exit_routine;
}
if ($result != 1)
{
	print "Command line options invalid, exiting program\n";
	$daemon = 1;
	&exit_routine;
}
#----------------------------------------------------------------------------------#
# Now that we've determined that the user specified all the required parms
#      and passed reasonable values with them, issue the chswnm command to check
#      if the daemon running
$chswnm_return = `$chswnm_Q`;

# Check return code from chswnm 
if ( ($chswnm_return != 1) && ($chswnm_return !=2) && ($chswnm_return !=3) ) {
        # Non-zero return code from i_stub_FS 
        #print "Daemon is running...exit\n"; #DEBUG
	print "Daemon already running ... exiting\n"; 
                $daemon = 1;	#ret 1 for defect 109668, signal that daemon running on system 
                &exit_routine;
       
}
if ($chswnm_return == 3)			#return code of 3, there was an error
{
	$ret =2;
	&exit_routine;
}
$chswnm_return = `$chswnm_C`;		#store return code after executed

if ($chswnm_return != 0)		#returns 0 if successful
{
        $ret =2;
        &exit_routine;
}
$count = 0;
$i_stub_flag = 0;
open(FILE, "$i_stub_FS |") or die "Can't open i_stub_FS: $!\n";
                while(<FILE>) {                         #open file, read, store in variable
                chomp $_;				#chomp new lines from file
		if ($count == 0)
		{
			@split_array  = unpack("A1" x length($_), $_);	#unpack everything into array by char
			if (@split_array[3] eq "")			#if third element is blank than check code
			{
        			$i_stub_return = @split_array[0];	#build return code	
				$i_stub_return .= @split_array[1];
			}
			if ( ($i_stub_return == 50) || ($i_stub_return == 57) )	#if 50/57 then check i_stub_msg.pl
			{
				&i_stub_msg;
				print $RC_MSG;				#print msg
				$i_stub_flag = 1;			#set flag and exit
				&exit_routine;
			}
			else						#if no return code then print heading
			{
				print "frame\tcage\ttype\tlocation\tname\n";
			}
}
			$_ .= "&";					#add & to each line end
			@split_array  = unpack("A1" x length($_), $_);	#unpack everything by char into arry
			$count += 1;					#bump line counter
			$i = 0;
                        while (@split_array[$i] ne "&" )		#while we did not reach an & on line
			{
				if (@split_array[$i] ne "&")		
				{
					print "@split_array[$i]";	#print char to screen
				}
				if ( (@split_array[$i] eq "") && (@split_array[$i+1] ne "(") )	#this is formatting issue
				{
					print " ";
					print "\t";
				}
				$i = $i + 1;					#bump element in array
			}
			print "\n";						#print new line to screen
			#print "$_\n";			#display output to Stdout
                }
                close(FILE);

&exit_routine;
#----------------------------------------------------------------------------------#
